﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ShopCart
/// </summary>
public class MyShopCart
{
  public List<ProductShop> ListProduct { get; set; }

  public MyShopCart()
  {
    ListProduct = new List<ProductShop>();
  }

  public void Add(ProductShop product)
  {
    if (!CheckExits(product.Size, product.Colour))
    {
      ListProduct.Add(product);
    }
    else
    {
        for (int i = 0; i < ListProduct.Count; i++)
        {
            if (ListProduct[i].Size == product.Size && ListProduct[i].Colour == product.Colour && ListProduct[i].Id == product.Id)
            {
                ListProduct[i].Quantity = product.Quantity;
                break;
            }
        }
    }
  }

  public void Delete(string id)
  {
    ProductShop item = ListProduct.Where(p => p.Id == id).FirstOrDefault();
    if (item != null)
      ListProduct.Remove(item);    
  }

  public void AddQuantity(string id, int qty, int size, int color)
  {
    ProductShop item = ListProduct.Where(p => p.Id == id).FirstOrDefault();
    if (item != null)
    {
        item.Quantity = qty;
        item.Colour = color;
        item.Size = size;
    }
  }

  public void UpdateQuantity(string id, int quantity, int size, int color)
  {
    ProductShop item = ListProduct.Where(p => p.Id == id).FirstOrDefault();
    if (item != null)
    {
        item.Quantity = quantity;
        item.Colour = color;
        item.Size = size;
    }
  }

  private bool CheckExits(int size, int Color)
  {
    foreach (ProductShop product in ListProduct)
    {
        if (product.Size == size && product.Colour == Color)
            return true;
    }
    return false;
  }

  public decimal GetTotalCost()
  {
    decimal cost = 0;
    foreach (ProductShop product in ListProduct)
    {
      cost += (product.Cost * product.Quantity);
    }
    return cost;
  }
  public decimal GetCostQty(string Id, int size, int color)
  {
      decimal costTotal = 0;
      ProductShop product = ListProduct.Where(p => p.Id == Id && p.Size == size && p.Colour == color).FirstOrDefault();
      decimal cost = product.Cost;
      int qty = product.Quantity;
      costTotal = cost * qty;
      return costTotal;
  }
  public int GetTotalProduct()
  {
    int total = 0;
    foreach (ProductShop product in ListProduct)
    {
      total += product.Quantity;
    }
    return total;
  }
}